#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

void toksplit(string & str,const char *delim, vector<string> &ans){
    if(str.size()<1) return ;
    char * pstr = new char[str.size()+1];
    memset(pstr,0x00,str.size()+1);
    strncpy(pstr,str.c_str(),str.size());
    char *p =strtok(pstr,delim);
    while (p)
    {
        ans.emplace_back(p);
        p = strtok(nullptr,delim);
    }  
    delete [] pstr;
}

void subsplit(string & str,const char *delim,vector<string> &ans){
    size_t st=0,end=0;
    while(st<str.size()&&(end=str.find(delim,st))!=string::npos){
        if(end!=st){
            ans.emplace_back(str,st,end-st);
        }
        st =end+1;
    }
    if(end!=st){
        ans.emplace_back(str,st,str.size()-st);
    }
}

void findsplit(string & str,const char *delim,vector<string> &ans){
    size_t st = str.find_first_not_of(delim),end=0;
    while((end=str.find_first_of(delim,st))!=string::npos){
            ans.emplace_back(str,st,end-st);
            st = str.find_first_not_of(delim,end);
    }
    if(st!=string::npos){
        ans.emplace_back(str,st,str.size()-st);
    }
}

void strstreamsplit(string & str,vector<string> &ans){
    istringstream iss(str,istringstream::in);
    string temp;
    while(iss>>temp){
        ans.emplace_back(temp);
    }

}

int main(){
    string t1 = "  asc   ii   i,m pandas!  ";
    string t2 = "123 456 I Love Pandas";
    string t3 ="   ";
    string t4 ="";
    string t5 ="Pandas";
    char * delim =" ";
    vector<string> ans;
    toksplit(t1,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    toksplit(t2,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;   
    ans.clear();
    toksplit(t3,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    toksplit(t4,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    toksplit(t5,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    cout<<"开始find分割"<<endl;
    subsplit(t1,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    subsplit(t2,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    subsplit(t3,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    subsplit(t4,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    subsplit(t5,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    cout<<"开始find_first 分割"<<endl;
    findsplit(t1,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    findsplit(t2,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    findsplit(t3,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    findsplit(t4,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    findsplit(t5,delim,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();
    cout<<"使用stringstreams"<<endl;
    strstreamsplit(t1,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();   
    strstreamsplit(t2,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();   
    strstreamsplit(t3,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();   
    strstreamsplit(t4,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();   
    strstreamsplit(t5,ans);
    for_each(ans.begin(),ans.end(),[](string &s){cout<<s<<"--";});
    cout<<endl;
    ans.clear();   
}